<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>RF22: RF22ReliableDatagram.h Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />



</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">RF22
   
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5.1 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
    </ul>
  </div>
<div class="header">
  <div class="headertitle">
<div class="title">RF22ReliableDatagram.h</div>  </div>
</div>
<div class="contents">
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// RF22ReliableDatagram.h</span>
<a name="l00002"></a>00002 <span class="comment">//</span>
<a name="l00003"></a>00003 <span class="comment">// Author: Mike McCauley (mikem@open.com.au)</span>
<a name="l00004"></a>00004 <span class="comment">// Copyright (C) 2011 Mike McCauley</span>
<a name="l00005"></a>00005 <span class="comment">// $Id: RF22ReliableDatagram.h,v 1.6 2011/02/15 01:18:03 mikem Exp $</span>
<a name="l00006"></a>00006 
<a name="l00007"></a>00007 <span class="preprocessor">#ifndef RF22ReliableDatagram_h</span>
<a name="l00008"></a>00008 <span class="preprocessor"></span><span class="preprocessor">#define RF22ReliableDatagram_h</span>
<a name="l00009"></a>00009 <span class="preprocessor"></span>
<a name="l00010"></a>00010 <span class="preprocessor">#include &lt;RF22Datagram.h&gt;</span>
<a name="l00011"></a>00011 
<a name="l00012"></a>00012 <span class="comment">// The acknowledgement bit in the FLAGS</span>
<a name="l00013"></a>00013 <span class="preprocessor">#define RF22_FLAGS_ACK 0x80</span>
<a name="l00014"></a>00014 <span class="preprocessor"></span><span class="comment"></span>
<a name="l00015"></a>00015 <span class="comment">/////////////////////////////////////////////////////////////////////</span>
<a name="l00016"></a>00016 <span class="comment">/// \class RF22ReliableDatagram RF22ReliableDatagram.h &lt;RF22ReliableDatagram.h&gt;</span>
<a name="l00017"></a>00017 <span class="comment">/// \brief RF22 subclass for sending addressed, acknowledged, retransmitted datagrams.</span>
<a name="l00018"></a>00018 <span class="comment">///</span>
<a name="l00019"></a>00019 <span class="comment">/// Extends RF22Datagram to define addressed, reliable datagrams with acknowledgement and retransmission.</span>
<a name="l00020"></a>00020 <span class="comment">/// Based on RF22Datagram, adds flags and sequence numbers. RF22ReliableDatagram is reliable in the sense</span>
<a name="l00021"></a>00021 <span class="comment">/// that messages are acknowledged, and unacknowledged messages are retransmitted until acknowledged or the</span>
<a name="l00022"></a>00022 <span class="comment">/// retries are exhausted.</span>
<a name="l00023"></a>00023 <span class="comment">/// When addressed messages are sent (by sendtoWait()), it will wait for an ack, and retransmit</span>
<a name="l00024"></a>00024 <span class="comment">/// after timeout until an ack is received or retries are exhausted.</span>
<a name="l00025"></a>00025 <span class="comment">/// When addressed messages are collected by the application (by recvfromAck()), </span>
<a name="l00026"></a>00026 <span class="comment">/// an acknowledgement is automatically sent.</span>
<a name="l00027"></a>00027 <span class="comment">///</span>
<a name="l00028"></a>00028 <span class="comment">/// The retransmit timeout is randomly varied between timeout and timeout*2 to prevent collisions on all</span>
<a name="l00029"></a>00029 <span class="comment">/// retries when  2 nodes happen to start sending at the same time .</span>
<a name="l00030"></a>00030 <span class="comment">///</span>
<a name="l00031"></a>00031 <span class="comment">/// Each new message sent by sendtoWait() has its ID incremented.</span>
<a name="l00032"></a>00032 <span class="comment">///</span>
<a name="l00033"></a>00033 <span class="comment">/// An ack consists of a message with:</span>
<a name="l00034"></a>00034 <span class="comment">/// - TO set to the from address of the original message</span>
<a name="l00035"></a>00035 <span class="comment">/// - FROM set to this node address</span>
<a name="l00036"></a>00036 <span class="comment">/// - ID set to the ID of the original message</span>
<a name="l00037"></a>00037 <span class="comment">/// - FLAGS with the RF22_FLAGS_ACK bit set</span>
<a name="l00038"></a>00038 <span class="comment">///</span>
<a name="l00039"></a>00039 <span class="comment">/// Part of the Arduino RF22 library for operating with HopeRF RF22 compatible transceivers </span>
<a name="l00040"></a>00040 <span class="comment">/// (see http://www.hoperf.com)</span>
<a name="l00041"></a><a class="code" href="classRF22ReliableDatagram.html">00041</a> <span class="comment"></span><span class="keyword">class </span><a class="code" href="classRF22ReliableDatagram.html" title="RF22 subclass for sending addressed, acknowledged, retransmitted datagrams.">RF22ReliableDatagram</a> : <span class="keyword">public</span> <a class="code" href="classRF22Datagram.html" title="RF22 subclass for addressed, unreliable messages.">RF22Datagram</a>
<a name="l00042"></a>00042 {
<a name="l00043"></a>00043 <span class="keyword">public</span>:<span class="comment"></span>
<a name="l00044"></a>00044 <span class="comment">    /// Constructor. </span>
<a name="l00045"></a>00045 <span class="comment">    /// \param[in] thisAddress The address to assign to this node. Defaults to 0</span>
<a name="l00046"></a>00046 <span class="comment">    /// \param[in] slaveSelectPin the Arduino pin number of the output to use to select the RF22 before</span>
<a name="l00047"></a>00047 <span class="comment">    /// accessing it</span>
<a name="l00048"></a>00048 <span class="comment">    /// \param[in] interrupt The interrupt number to use. Default is interrupt 0 (Arduino input pin 2)</span>
<a name="l00049"></a>00049 <span class="comment"></span>    <a class="code" href="classRF22ReliableDatagram.html#ac5c5c271003d79f6ae268693087eb224">RF22ReliableDatagram</a>(uint8_t thisAddress = 0, uint8_t slaveSelectPin = 10, uint8_t interrupt = 0);
<a name="l00050"></a>00050 <span class="comment"></span>
<a name="l00051"></a>00051 <span class="comment">    /// Sets the minimum retransmit timeout. If sendtoWait is waiting for an ack </span>
<a name="l00052"></a>00052 <span class="comment">    /// longer than this time (in milliseconds), </span>
<a name="l00053"></a>00053 <span class="comment">    /// it will retransmit the message. Defaults to 200ms. The timeout is measured from the end of</span>
<a name="l00054"></a>00054 <span class="comment">    /// transmission of the message. It must be at least longer than the the transmit </span>
<a name="l00055"></a>00055 <span class="comment">    /// time of the acknowledgement (6 octets) plus the latency/poll time of the receiver. </span>
<a name="l00056"></a>00056 <span class="comment">    /// The actual timeout is randomly varied between timeout and timeout*2.</span>
<a name="l00057"></a>00057 <span class="comment">    /// \param[in] timeout The new timeout period in milliseconds</span>
<a name="l00058"></a>00058 <span class="comment"></span>    <span class="keywordtype">void</span> <a class="code" href="classRF22ReliableDatagram.html#a57e6fe9fec497df332ce1f533bf2b2ec">setTimeout</a>(uint16_t timeout);
<a name="l00059"></a>00059 <span class="comment"></span>
<a name="l00060"></a>00060 <span class="comment">    /// Sets the max number of retries. Defaults to 3. If set to 0, the message will only be sent once.</span>
<a name="l00061"></a>00061 <span class="comment">    /// sendtoWait will give up and return false if there is no ack received after all transmissions time out.</span>
<a name="l00062"></a>00062 <span class="comment">    /// param[in] retries The maximum number a retries.</span>
<a name="l00063"></a>00063 <span class="comment"></span>    <span class="keywordtype">void</span> <a class="code" href="classRF22ReliableDatagram.html#add0bf2e27e9ea07871dbe9689dcef465">setRetries</a>(uint8_t retries);
<a name="l00064"></a>00064 <span class="comment"></span>
<a name="l00065"></a>00065 <span class="comment">    /// Send the message and waits for an ack. Returns true if an acknowledgement is received.</span>
<a name="l00066"></a>00066 <span class="comment">    /// Synchronous: any message other than the desired ACK received while waiting is discarded.</span>
<a name="l00067"></a>00067 <span class="comment">    /// Blocks until an ACK is received or all retries are exhausted (ie up to retries*timeout milliseconds).</span>
<a name="l00068"></a>00068 <span class="comment">    /// \param[in] address The address to send the message to.</span>
<a name="l00069"></a>00069 <span class="comment">    /// \param[in] buf Pointer to the binary message to send</span>
<a name="l00070"></a>00070 <span class="comment">    /// \param[in] len Number of octets to send</span>
<a name="l00071"></a>00071 <span class="comment">    /// \return true if the message was transmitted and an acknowledgement was received.</span>
<a name="l00072"></a>00072 <span class="comment"></span>    <span class="keywordtype">boolean</span> <a class="code" href="classRF22ReliableDatagram.html#a47e85bffb81a3acdff6d5f15ce7bb65e">sendtoWait</a>(uint8_t* buf, uint8_t len, uint8_t address);
<a name="l00073"></a>00073 <span class="comment"></span>
<a name="l00074"></a>00074 <span class="comment">    /// If there is a valid message available for this node, send an acknowledgement to the SRC</span>
<a name="l00075"></a>00075 <span class="comment">    /// address (blocking until this is complete), then copy the message to buf and return true</span>
<a name="l00076"></a>00076 <span class="comment">    /// else return false. </span>
<a name="l00077"></a>00077 <span class="comment">    /// If a message is copied, *len is set to the length..</span>
<a name="l00078"></a>00078 <span class="comment">    /// If from is not NULL, the SRC address is placed in *from.</span>
<a name="l00079"></a>00079 <span class="comment">    /// If to is not NULL, the DEST address is placed in *to.</span>
<a name="l00080"></a>00080 <span class="comment">    /// This is the preferred function for getting messages addressed to this node.</span>
<a name="l00081"></a>00081 <span class="comment">    /// If the message is not a broadcast, acknowledge to the sender before returning.</span>
<a name="l00082"></a>00082 <span class="comment">    /// You should be sure to call this function frequently enough to not miss any messages</span>
<a name="l00083"></a>00083 <span class="comment">    /// It is recommended that you call it in your main loop.</span>
<a name="l00084"></a>00084 <span class="comment">    /// \param[in] buf Location to copy the received message</span>
<a name="l00085"></a>00085 <span class="comment">    /// \param[in,out] len Available space in buf. Set to the actual number of octets copied.</span>
<a name="l00086"></a>00086 <span class="comment">    /// \param[in] from If present and not NULL, the referenced uint8_t will be set to the SRC address</span>
<a name="l00087"></a>00087 <span class="comment">    /// \param[in] to If present and not NULL, the referenced uint8_t will be set to the DEST address</span>
<a name="l00088"></a>00088 <span class="comment">    /// \param[in] id If present and not NULL, the referenced uint8_t will be set to the ID</span>
<a name="l00089"></a>00089 <span class="comment">    /// \param[in] flags If present and not NULL, the referenced uint8_t will be set to the FLAGS</span>
<a name="l00090"></a>00090 <span class="comment">    /// (not just those addressed to this node).</span>
<a name="l00091"></a>00091 <span class="comment">    /// \return true if a valid message was copied to buf</span>
<a name="l00092"></a>00092 <span class="comment"></span>    <span class="keywordtype">boolean</span> <a class="code" href="classRF22ReliableDatagram.html#a49dbf986b90cac7f1476ec56a4beab38">recvfromAck</a>(uint8_t* buf, uint8_t* len, uint8_t* from = NULL, uint8_t* to = NULL, uint8_t* <span class="keywordtype">id</span> = NULL, uint8_t* flags = NULL);
<a name="l00093"></a>00093 <span class="comment"></span>
<a name="l00094"></a>00094 <span class="comment">    /// Similar to recvfromAck(), this will block until either a valid message available for this node</span>
<a name="l00095"></a>00095 <span class="comment">    /// or the timeout expires. Starts the receiver automatically.</span>
<a name="l00096"></a>00096 <span class="comment">    /// You should be sure to call this function frequently enough to not miss any messages</span>
<a name="l00097"></a>00097 <span class="comment">    /// It is recommended that you call it in your main loop.</span>
<a name="l00098"></a>00098 <span class="comment">    /// \param[in] buf Location to copy the received message</span>
<a name="l00099"></a>00099 <span class="comment">    /// \param[in,out] len Available space in buf. Set to the actual number of octets copied.</span>
<a name="l00100"></a>00100 <span class="comment">    /// \param[in] timeout Maximum time to wait in milliseconds</span>
<a name="l00101"></a>00101 <span class="comment">    /// \param[in] from If present and not NULL, the referenced uint8_t will be set to the SRC address</span>
<a name="l00102"></a>00102 <span class="comment">    /// \param[in] to If present and not NULL, the referenced uint8_t will be set to the DEST address</span>
<a name="l00103"></a>00103 <span class="comment">    /// \param[in] id If present and not NULL, the referenced uint8_t will be set to the ID</span>
<a name="l00104"></a>00104 <span class="comment">    /// \param[in] flags If present and not NULL, the referenced uint8_t will be set to the FLAGS</span>
<a name="l00105"></a>00105 <span class="comment">    /// (not just those addressed to this node).</span>
<a name="l00106"></a>00106 <span class="comment">    /// \return true if a valid message was copied to buf</span>
<a name="l00107"></a>00107 <span class="comment"></span>    <span class="keywordtype">boolean</span> <a class="code" href="classRF22ReliableDatagram.html#a0327f5c2c296c483df90b1f52ec2d141">recvfromAckTimeout</a>(uint8_t* buf, uint8_t* len,  uint16_t timeout, uint8_t* from = NULL, uint8_t* to = NULL, uint8_t* <span class="keywordtype">id</span> = NULL, uint8_t* flags = NULL);
<a name="l00108"></a>00108 <span class="comment"></span>
<a name="l00109"></a>00109 <span class="comment">    /// Returns the number of retransmissions </span>
<a name="l00110"></a>00110 <span class="comment">    /// we have had to send</span>
<a name="l00111"></a>00111 <span class="comment">    /// \return The number of retransmissions since initialisation.</span>
<a name="l00112"></a>00112 <span class="comment"></span>    uint16_t <a class="code" href="classRF22ReliableDatagram.html#a797ad6d1face3b9c4e8862e202a26e5b">retransmissions</a>();
<a name="l00113"></a>00113 
<a name="l00114"></a>00114 <span class="keyword">protected</span>:<span class="comment"></span>
<a name="l00115"></a>00115 <span class="comment">    /// Send an ACK for the message id to the given from address</span>
<a name="l00116"></a>00116 <span class="comment">    /// Blocks until the ACK has been sent</span>
<a name="l00117"></a>00117 <span class="comment"></span>    <span class="keywordtype">void</span> <a class="code" href="classRF22ReliableDatagram.html#aec55325c6ab74809a3ef9c90aef39adc">acknowledge</a>(uint8_t <span class="keywordtype">id</span>, uint8_t from);
<a name="l00118"></a>00118 <span class="comment"></span>
<a name="l00119"></a>00119 <span class="comment">    /// Checks whether the message currently in the Rx buffer is a new message, not previously received</span>
<a name="l00120"></a>00120 <span class="comment">    /// based on the from address and the sequence.  If it is new, it is acknowledged and returns true</span>
<a name="l00121"></a>00121 <span class="comment">    /// \return true if there is a message received and it is a new message</span>
<a name="l00122"></a>00122 <span class="comment"></span>    <span class="keywordtype">boolean</span> <a class="code" href="classRF22ReliableDatagram.html#a90288b60894ace02cea63f2cfa7a9c17">haveNewMessage</a>();
<a name="l00123"></a>00123 
<a name="l00124"></a>00124 <span class="keyword">private</span>:<span class="comment"></span>
<a name="l00125"></a>00125 <span class="comment">    /// Count of retransmissions we have had to send</span>
<a name="l00126"></a>00126 <span class="comment"></span>    uint16_t _retransmissions;
<a name="l00127"></a>00127 <span class="comment"></span>
<a name="l00128"></a>00128 <span class="comment">    /// The last sequence number to be used</span>
<a name="l00129"></a>00129 <span class="comment">    /// Defaults to 0</span>
<a name="l00130"></a>00130 <span class="comment"></span>    uint8_t _lastSequenceNumber;
<a name="l00131"></a>00131 
<a name="l00132"></a>00132     <span class="comment">// Retransmit timeout (milliseconds)</span><span class="comment"></span>
<a name="l00133"></a>00133 <span class="comment">    /// Defaults to 200</span>
<a name="l00134"></a>00134 <span class="comment"></span>    uint16_t _timeout;
<a name="l00135"></a>00135 
<a name="l00136"></a>00136     <span class="comment">// Retries (0 means one try only)</span><span class="comment"></span>
<a name="l00137"></a>00137 <span class="comment">    /// Defaults to 3</span>
<a name="l00138"></a>00138 <span class="comment"></span>    uint8_t _retries;
<a name="l00139"></a>00139 <span class="comment"></span>
<a name="l00140"></a>00140 <span class="comment">    /// Array of the last seen sequence number indexed by node address that sent it</span>
<a name="l00141"></a>00141 <span class="comment">    /// It is used for duplicate detection. Duplicated messages are re-acknowledged when received </span>
<a name="l00142"></a>00142 <span class="comment">    /// (this is generally due to lost ACKs, causing the sender to retransmit, even though we have already</span>
<a name="l00143"></a>00143 <span class="comment">    /// received that message)</span>
<a name="l00144"></a>00144 <span class="comment"></span>    uint8_t _seenIds[256];
<a name="l00145"></a>00145 
<a name="l00146"></a>00146 
<a name="l00147"></a>00147 };
<a name="l00148"></a>00148 
<a name="l00149"></a>00149 <span class="preprocessor">#endif</span>
<a name="l00150"></a>00150 <span class="preprocessor"></span>
</pre></div></div>
</div>


<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.5.1
</small></address>

</body>
</html>
